﻿using Domain.Models;
using IServices;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using ViewModel.Response;

namespace Management.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class DictsController : BaseController
    {
        private readonly IDictsService dictsService;

        public DictsController(IDictsService dictsService,
            ILogger<DictsController> logger) : base(logger)
        {
            this.dictsService = dictsService;
        }
        /// <summary>
        /// 获取所有字典数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ResponseModel<IEnumerable<Dicts>> GetDicts()
        {
            var result = new ResponseModel<IEnumerable<Dicts>>
            {
                Data = dictsService.GetDicts(),
                Status = ResponseStatus.OK
            };
            return result;
        }
        /// <summary>
        /// 获取字典数据
        /// </summary>
        /// <param name="name">字典名称</param>
        /// <returns></returns>
        [HttpGet("{name}")]
        public ResponseModel<Dicts> GetDicts(string name)
        {
            var result = new ResponseModel<Dicts>
            {
                Status = ResponseStatus.OK,
                Data = dictsService.GetDicts(name),
            };
            return result;
        }

        /// <summary>
        /// 添加字典数据
        /// </summary>
        /// <param name="dicts">字典实体</param>
        /// <returns></returns>
        [HttpPost]
        public ResponseModel<string> AddDicts(Dicts dicts)
        {
            var result = new ResponseModel<string>
            {
                Status = ResponseStatus.Fail
            };
            if (this.dictsService.Add(dicts) > 0)
            {
                result.Data = dicts.Id.ToString();
                result.Status = ResponseStatus.OK;
            }
            return result;
        }

        /// <summary>
        /// 删除字典
        /// </summary>
        /// <param name="id">字典ID</param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public ResponseModel<string> RemoveDicts(int id)
        {
            var result = new ResponseModel<string>
            {
                Status = ResponseStatus.Fail
            };
            var dicts = this.dictsService.QueryById(id);
            if (dicts == null)
            {
                result.Status = ResponseStatus.NotFound;
                return result;
            }
            if (this.dictsService.DeleteById(id))
            {
                result.Status = ResponseStatus.OK;
                result.Data = dicts.Id.ToString();
            }
            return result;
        }
    }
}